package pri.jz.keeper.ui.main

import android.content.Intent
import androidx.activity.viewModels
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import pri.jz.keeper.ui.content.ContentActivity
import pri.jz.keeper.ui.base.BaseActivity
import pri.jz.keeper.ui.view.MainView
import pri.jz.keeper.util.NetworkUtil
import pri.jz.keeper.ui.base.BaseViewModel
import pri.jz.keeper.ui.login.LoginActivity
import pri.jz.keeper.util.ActivityUtil

class MainActivity : BaseActivity() {
    private val mViewModel: MainViewModel by viewModels()

    override fun initData() {
        super.initData()
        mViewModel.initData()
    }

    @Composable
    override fun OnCreateView() {
        fullScreen(false)
        NetworkUtil.networkState = remember { mutableStateOf(1) }
        MainView()
    }

    override suspend fun onObserver() {
        mViewModel.mainUiState.collect {
            when(it) {
                is MainUiState.DEFAULT -> {}
                is MainUiState.LOGIN -> {
                    startActivity(Intent(this, LoginActivity::class.java))
                    finish()
                }
                is MainUiState.CONTENT -> {
                    startActivity(Intent(this, ContentActivity::class.java).apply {
                        putExtra("userId",it.userId)
                    })
                    finish()
                }
                is MainUiState.EXIT -> ActivityUtil.exitApp(this)
            }
        }
    }

    override fun getViewModel(): BaseViewModel = mViewModel

}
